package com.noocodecommit.wicket.jquery;

import org.apache.wicket.PageParameters;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;

import com.noocodecommit.wicket.jquery.ajax.AjaxPage;
import com.noocodecommit.wicket.jquery.plugin.autodefaulttextfield.AutoDefaultTextFieldPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.blind.BlindPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.bounce.BouncePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.clip.ClipPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.drop.DropPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.explode.ExplodePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.fold.FoldPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.highlight.HighlightPage;
import com.noocodecommit.wicket.jquery.plugin.enchant.pulsate.PulsatePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.scale.ScalePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.shake.ShakePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.slide.SlidePage;
import com.noocodecommit.wicket.jquery.plugin.enchant.transfer.TransferPage;
import com.noocodecommit.wicket.jquery.plugin.lightbox.LightboxPage;
import com.noocodecommit.wicket.jquery.plugin.poll.PollPage;
import com.noocodecommit.wicket.jquery.plugin.ui.accordion.AccordionPage;
import com.noocodecommit.wicket.jquery.plugin.ui.autocomplete.AutocompletePage;
import com.noocodecommit.wicket.jquery.plugin.ui.colorpicker.ColorPickerPage;
import com.noocodecommit.wicket.jquery.plugin.ui.datepicker.DatePickerPage;
import com.noocodecommit.wicket.jquery.plugin.ui.dialog.DialogPage;
import com.noocodecommit.wicket.jquery.plugin.ui.draggable.DraggablePage;
import com.noocodecommit.wicket.jquery.plugin.ui.droppable.DroppablePage;
import com.noocodecommit.wicket.jquery.plugin.ui.magnifier.MagnifierPage;
import com.noocodecommit.wicket.jquery.plugin.ui.progressbar.ProgressBarPage;
import com.noocodecommit.wicket.jquery.plugin.ui.resizable.ResizablePage;
import com.noocodecommit.wicket.jquery.plugin.ui.selectable.SelectablePage;
import com.noocodecommit.wicket.jquery.plugin.ui.slider.SliderPage;
import com.noocodecommit.wicket.jquery.plugin.ui.sortable.SortablePage;
import com.noocodecommit.wicket.jquery.plugin.ui.spinner.SpinnerPage;
import com.noocodecommit.wicket.jquery.plugin.ui.tabs.TabsPage;
import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChili;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChiliPlugin;
import com.noocodecommit.wicket.stuff.jquery.plugin.effects.blind.JQBlindPlugin;
import com.noocodecommit.wicket.stuff.jquery.theme.ITheme;
import com.noocodecommit.wicket.stuff.jquery.theme.corpcool.CorpCoolTheme;
import com.noocodecommit.wicket.stuff.jquery.theme.cupertino.CupertinoTheme;
import com.noocodecommit.wicket.stuff.jquery.theme.neoflora.NeoFloraTheme;

public class TemplatePage extends WebPage
{
	private static final long serialVersionUID = 1L;

	public TemplatePage()
	{
		this(null);
	}

	public TemplatePage(PageParameters parameters)
	{
		super(parameters);
		Session.get();
		add(new BookmarkablePageLink("lnkHome", HomePage.class));

		add(new BookmarkablePageLink("lnkAjax", AjaxPage.class));

		add(new BookmarkablePageLink("lnkDraggable", DraggablePage.class));
		add(new BookmarkablePageLink("lnkDroppable", DroppablePage.class));
		add(new BookmarkablePageLink("lnkSortable", SortablePage.class));
		add(new BookmarkablePageLink("lnkSelectable", SelectablePage.class));
		add(new BookmarkablePageLink("lnkResizable", ResizablePage.class));

		add(new BookmarkablePageLink("lnkAccordion", AccordionPage.class));
		add(new BookmarkablePageLink("lnkAutocomplete", AutocompletePage.class));
		add(new BookmarkablePageLink("lnkColorPicker", ColorPickerPage.class));
		add(new BookmarkablePageLink("lnkMagnifier", MagnifierPage.class));
		add(new BookmarkablePageLink("lnkProgressBar", ProgressBarPage.class));
		add(new BookmarkablePageLink("lnkSpinner", SpinnerPage.class));
		add(new BookmarkablePageLink("lnkSlider", SliderPage.class));
		add(new BookmarkablePageLink("lnkTabs", TabsPage.class));
		add(new BookmarkablePageLink("lnkDatePicker", DatePickerPage.class));
		add(new BookmarkablePageLink("lnkDialog", DialogPage.class));

		add(new BookmarkablePageLink("lnkLightbox", LightboxPage.class));
		add(new BookmarkablePageLink("lnkAutoDefaultTextField", AutoDefaultTextFieldPage.class));
		add(new BookmarkablePageLink("lnkPoll", PollPage.class));

		add(new BookmarkablePageLink("lnkBlind", BlindPage.class));
		add(new BookmarkablePageLink("lnkBounce", BouncePage.class));
		add(new BookmarkablePageLink("lnkClip", ClipPage.class));
		add(new BookmarkablePageLink("lnkDrop", DropPage.class));
		add(new BookmarkablePageLink("lnkExplode", ExplodePage.class));
		add(new BookmarkablePageLink("lnkFold", FoldPage.class));
		add(new BookmarkablePageLink("lnkHighlight", HighlightPage.class));
		add(new BookmarkablePageLink("lnkPulsate", PulsatePage.class));
		add(new BookmarkablePageLink("lnkScale", ScalePage.class));
		add(new BookmarkablePageLink("lnkShake", ShakePage.class));
		add(new BookmarkablePageLink("lnkSlide", SlidePage.class));
		add(new BookmarkablePageLink("lnkTransfer", TransferPage.class));

		add(new AjaxLink("lnkNeoFlora")
		{
			public void onClick(AjaxRequestTarget arg0)
			{
				ITheme theme = new NeoFloraTheme();
				((JQuery4WicketSession) getSession()).setTheme(theme);
				JQCore.get().setTheme(theme);
				// ((JQuery4WicketSession) getSession()).setTheme("neoflora");
				// JQCore.get().setTheme(((JQuery4WicketSession) getSession()).getTheme());
				setResponsePage(HomePage.class);
			}

		});
		add(new AjaxLink("lnkCupertino")
		{
			public void onClick(AjaxRequestTarget arg0)
			{
				ITheme theme = new CupertinoTheme();
				((JQuery4WicketSession) getSession()).setTheme(theme);
				JQCore.get().setTheme(theme);
				// JQuery4WicketSession.get().setTheme("cupertino");
				// JQCore.get().setTheme(JQuery4WicketSession.get().getTheme());
				setResponsePage(HomePage.class);
			}

		});
		add(new AjaxLink("lnkCorpCool")
		{
			public void onClick(AjaxRequestTarget arg0)
			{
				ITheme theme = new CorpCoolTheme();
				((JQuery4WicketSession) getSession()).setTheme(theme);
				JQCore.get().setTheme(theme);
				// JQuery4WicketSession.get().setTheme("corpcool");
				// JQCore.get().setTheme(JQuery4WicketSession.get().getTheme());
				setResponsePage(HomePage.class);
			}

		});
		JQCore.setLibraryMode(JQCore.LIBMODE_MINIFIED);
		JQCore.init(this);
		JQCore.get().setTheme(JQuery4WicketSession.get().getTheme());

		this.add(HeaderContributor.forCss("css/jquery/ui/themes/" + JQCore.get().getTheme().getName() + "/style.css"));
	}

	protected void setupChili()
	{
		JQCore.get().bindPlugin(new JQBlindPlugin());
		JQChiliPlugin chiliPlugin = new JQChiliPlugin(JQChiliPlugin.DEFAULT_KEY);
		// -- Chili plugin init selector : ".section > pre > code"
		chiliPlugin.setInitOnceSelector(".section > pre > code");
		// -- Chili options
		chiliPlugin.getOptions().setLineNumbers(true);
		// -- JQuery DOM ready script extend
		String chiliScript = "$('pre').hide();$('.section h5').css({cursor: 'pointer'}).bind('click', function(){$(this).parent().find('pre').toggle('blind',{direction:'vertical'}, 400);});";
		JQCore.get().getAfterPluginInitScripts().add(chiliScript);

	}

	protected void bindChiliCode(JQChili chili, Class<?> scope, String language)
	{
		JQCore.get().getBeforePluginInitScripts().add(
				"$.ajax({dataType:'text', url:'src/" + scope.getPackage().getName().replace(".", "/") + "/" + scope.getSimpleName() + "." + language + "', success:function(data){$('#" + chili.getMarkupId() + "').text(data);}});");
	}
}
